/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/* IPreferenceControl.m */

#import "IPreferenceControl.h"
#import <appkit/appkit.h>
#import <defaults/defaults.h>
#import <string.h>
#import <libc.h>
#import <objc/hashtable.h>
#import "errdebug.h"
#import "data_types.h"
#import "Localization.h"

#define LoStr(key)      doLocalString(NULL,key,NULL)

@implementation IPreferenceControl

- init
{
    const char	*home_dir;
    char	*newsrc_dir;
    const char	*newsrc_file=".newsbaserc";
    char	*get_mail_address();
    
    NXDefaultsVector NewsBaseDefaults = {
	{NNTPSERVER, "localhost"},
	{NEWSRCFILE, ""},
	{ARTICLEBROWSERMODE, "Browser"},
	{NEWSGROUPBROWSERMODE, "Browser"},
	{ARTICLESELECT, SINGLECLICK},
	{NUM_ARTTOGET, "20"},
	{ARTICLESIZEMARK, "0:100:300"},
	{RECONNECTTIME, "30"},
        {KANJICODE, "JIS"},
	{NNTPBROWSER_FRAME, "(128,373):(740,458):(737,76):(737,330):(290,330):(439,330)"},
	{NULL}
    };
    
    [super init];

    if(!(NXRegisterDefaults(OWNER, NewsBaseDefaults))) {
	NXRunAlertPanel(LoStr("NewsBase"),LoStr("can't open default database")
	,LoStr("OK"),NULL,NULL);
    }
    
    /* if value for NEWSRCFILE =="", $HOME/.newsbaserc is set */
    if (!(strcmp(NXGetDefaultValue(OWNER, NEWSRCFILE),""))) {
	home_dir = NXHomeDirectory();
	newsrc_dir = (char *)NXZoneMalloc([self zone], 
		    		strlen(home_dir)+strlen(newsrc_file)+2);
	strcpy (newsrc_dir, home_dir); 
	strcat (newsrc_dir, "/"); 
	strcat (newsrc_dir, newsrc_file);
	NXSetDefault (OWNER, NEWSRCFILE, newsrc_dir);
    }
    return self;
}

- windowDidBecomeKey:sender
{
    [self read:self];
    return self;
}

- write:sender
{
    const char	*newsrc_dir;
    char	buf[64];
    
    /* check save and newsrc file directory */
    newsrc_dir = [oNewsrcFile stringValue];
    if (*newsrc_dir!='/') {
	NXRunAlertPanel(LoStr("NewsBase"),
	LoStr("please set absolute path name to directory"),
	LoStr("OK"),NULL,NULL);
	return NULL;
    }
    
    /* write value to database in local disk */
    NXWriteDefault(OWNER, NNTPSERVER, [oNntpServer stringValue]);
    NXWriteDefault(OWNER, RECONNECTTIME, 
    				[oReconnectTime stringValue]);
    NXWriteDefault(OWNER, KANJICODE, [[oKanjiCode selectedCell] title]);
    NXWriteDefault(OWNER, NEWSRCFILE, [oNewsrcFile stringValue]);
    NXWriteDefault(OWNER, ARTICLESELECT, 
    				[[oArticleSelect selectedCell] title]);
    NXWriteDefault(OWNER, NUM_ARTTOGET, [oNumArticleToGet stringValue]);

    sprintf(buf,"0:%.10d:%.10d", [oBoundaryTwo intValue], 
				    		[oBoundaryThree intValue]);
    NXWriteDefault(OWNER, ARTICLESIZEMARK, buf);
    
    return self;
}

- read:sender
{
    int		boundaryTwo, boundaryThree;
    const char	*buffer;
    
    [oNntpServer setStringValue:NXGetDefaultValue(OWNER, NNTPSERVER)];
    [oNewsrcFile setStringValue:NXGetDefaultValue(OWNER,NEWSRCFILE)];
    [self _selectCellMatrix:oArticleSelect
    			title:NXGetDefaultValue(OWNER, ARTICLESELECT)];
    [self _selectCellMatrix:oKanjiCode
    			title:NXGetDefaultValue(OWNER, KANJICODE)];
    [oNumArticleToGet setStringValue:NXGetDefaultValue(OWNER,NUM_ARTTOGET)];

    if ((buffer=NXGetDefaultValue(OWNER,ARTICLESIZEMARK)) != 0) {
	sscanf(buffer, "0:%d:%d", &boundaryTwo, &boundaryThree);
    }
    [oBoundaryTwo setIntValue:boundaryTwo];
    [oBoundaryThree setIntValue:boundaryThree];
    [oReconnectTime setStringValue:NXGetDefaultValue(OWNER,RECONNECTTIME)];
    return(self);
}

- (void)_selectCellMatrix:matrix title:(const char *)ktitle
{
    int		i;
    id		cell;
    
    if (ktitle == NULL) {
        [matrix selectCellAt:0 :0];
        return;
    }
    for (i=0; i<[matrix cellCount]; i++) {
	cell = [matrix cellAt:0 :i];
	if(!(strcmp([cell title], ktitle))) {
	    [matrix selectCell:cell];
	    break;
	}
    }
}

//- (char *)getMailAddress
//{
//    char	*mail_address;
//    char	*user_name, machine_name[256], domain_name[257];
//
//    user_name = NXCopyStringBufferFromZone(getenv("USER"),[self zone]);
//    gethostname(machine_name,sizeof(machine_name));
//    getdomainname(domain_name + 1, sizeof(domain_name) - 1);
//    domain_name[sizeof(domain_name) - 1] = '\0';
//    if (domain_name[1] != '\0') {
//        domain_name[0] = '.';
//    } else {
//        domain_name[0] = '\0';
//    }
//    mail_address = (char *)NXZoneMalloc([self zone],
//	    strlen(user_name)+strlen(machine_name)+strlen(domain_name)+3);
//    (void)sprintf(mail_address,"%s@%s%s",user_name,machine_name,
//							    domain_name);
//    return mail_address;
//}
//
//
@end
